import { v3, Vec2, Vec3, _decorator } from "cc";
import LevelColler from "../../MainScene/Script/LevelColler";
import Action3dManager, { ActionMngType } from "../../Script/Common/Action3dManager";
import GameConfig from "../../Script/GameSpecial/GameConfig";
import { EventType } from "../../Script/GameSpecial/GameEventType";
import { GlobalEnum } from "../../Script/GameSpecial/GlobalEnum";
import { Role } from "./Role";

const { ccclass, property } = _decorator;

/**玩家类，通过触摸控制器控制玩家角色 */
@ccclass('Player')
export class Player extends Role {

    public isPlayer: boolean = true;

    protected onEvents() {
        this.on(EventType.LevelEvent.moveJoy, this.onMoveJoy, this);
        this.on(EventType.LevelEvent.stopJoy, this.onStopJoy, this);
    }

    /**************************************************对外功能**************************************************/
    /**被击 */
    public beHit(atkRole: Role) {
        // this.exitLevel();
        Action3dManager.getMng(ActionMngType.Level).stopAllActions(this.node);
        // switch (atkRole.weaponType) {
        //     case GlobalEnum.WeaponType.captain:
        //     case GlobalEnum.WeaponType.gun:
        //     case GlobalEnum.WeaponType.hunter: {
        //         let str = "RoleDeadFlyAnim";
        //         let p = atkRole.node.position;
        //         let r = Math.atan2(p.z - this.node.position.z, this.node.position.x - p.x);
        //         this.emit(EventType.LevelEvent.killPlayer, { p: this.node.position, radian: r, scale: this.roleScale, isPlayer: true, skin: this.skinId, anim: str });
        //         break;
        //     }
        //     default: {
        //     }
        // }
        this.emit(EventType.LevelEvent.killPlayer, { p: this.node.position, radian: this.faceRadian, scale: this.roleScale, isPlayer: true, skin: this.skinId });
        this.disableAtkBox();
        // this.hide();
        this.node.active = false;
        this.isAlive = false;
        this.collider2d.density = 1000;
        this.collider2d.apply();
    }
    /**成功击中目标 */
    public hitSuccess(target: LevelColler) {
        let type = target.collerType;
        switch (type) {
            case GlobalEnum.CollerType.barrier: {
                break;
            }
            case GlobalEnum.CollerType.role: {
                this.growUpRoleScale();
                this.emit(EventType.LevelEvent.playerKillEnemy);
                this.emit(EventType.LevelEvent.updateRoleScale, this.roleScale);
                this.emit(EventType.LevelEvent.killOneRole, this);
                break;
            }
        }
    }
    /**复活 */
    public resurgence(pos: Vec3) {
        this.show();
        this.node.setPosition(pos);
        let rate = GameConfig.GameRule.rate3to2;
        this.rigidNode.setPosition(pos.x * rate, -pos.z * rate, 0);
        this.collider2d.radius = GameConfig.GameRule.roleRadius * GameConfig.GameRule.rate3to2 * this.roleScale;

        this.showAtkScopeNode();
        this.enableAtkBox();
        this.enterInvicible(3);
        this.enterCustomUpdateState(GlobalEnum.RoleState.stand);
        this.isAlive = true;
        this.collider2d.density = 1;
        this.collider2d.apply();
    }

    public getGold() {
        //材质球
        this.setMatGetGoldColor();
        //事件
        this.emit(EventType.LevelEvent.getGold);
        this.emit(EventType.Common.AudioEvent.playEffect, GlobalEnum.AudioClip.gold);
    }

    public playWinAnim() {
        this.resetInvicible();
        this.stopMove();
        this.node.eulerAngles = v3(0, -90, 0);
        // this.setRoleScale(GameConfig.GameRule.minScale);
        // this.growUpRoleScale(GameConfig.GameRule.minScale);
        this.roleScale = GameConfig.GameRule.minScale;
        let s = this.roleScale;
        let scale = Action3dManager.scaleTo(1, s, s, s);
        Action3dManager.getMng(ActionMngType.Level).runAction(this.node, scale);
        this.enterCustomUpdateState(GlobalEnum.RoleState.win);
        this.hideAtkScopeNode();
    }

    /**************************************************事件回调**************************************************/
    protected onMoveJoy(dir: Vec2) {
        // let angle = GlobalData.get(GlobalEnum.Common.GlobalDataType.levelCamera).node.eulerAngles.y;
        let angle = 0;
        let radian = Math.atan2(dir.y, dir.x) + angle * 0.017453;
        this.setMoveRadian(radian);
    }

    protected onStopJoy() {
        // this.stopMove();
        this.startAtk();
    }

}
